package com.gavindon.android.adapters

import android.view.FocusFinder
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gavindon.android.R
import com.gavindon.android.entity.ComboResp
import com.gavindon.android.entity.CourseOutputDTO
import com.gavindon.android.network.BASE_URL_IMG
import com.gavindon.android.widget.FocusViewUtils

/**
 * description: 套餐信息
 * Created by liNan on  2022-3-2 22:22
 */
class ComboLeftAdapter(layoutResId: Int, data: MutableList<ComboResp>?) :
    BaseQuickAdapter<ComboResp, BaseViewHolder>(layoutResId, data) {
    override fun convert(holder: BaseViewHolder, item: ComboResp) {
        holder.setText(R.id.tv_left, item.packageName)
        holder.itemView.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                item.packageId?.let {
                    focusListener?.invoke(item)
                }
            }
        }
    }

    private var focusListener: ((ComboResp) -> Unit)? = null
    fun setFocusListener(listener: (ComboResp) -> Unit) {
        this.focusListener = listener
    }
}

class ComboContentAdapter(layoutResId: Int, data: MutableList<CourseOutputDTO>?) :
    BaseQuickAdapter<CourseOutputDTO, BaseViewHolder>(layoutResId, data) {
    override fun convert(holder: BaseViewHolder, item: CourseOutputDTO) {
        holder.setText(R.id.ada_tv_title, item.courseName)
            .setText(R.id.ada_tv_price, "￥${item.presentPrice}")

        val iv = holder.getView<ImageView>(R.id.ada_iv_combo)
        Glide.with(holder.itemView)
            .load("$BASE_URL_IMG/${item.file.fileUrl}")
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(iv)
        FocusViewUtils.scaleView(holder.itemView, 0)

        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_DPAD_UP && event.action == KeyEvent.ACTION_DOWN) {
                val finder = FocusFinder.getInstance()
                val left = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_UP)
                if (left == null) {
                    dpadListener?.invoke(1)
                    return@setOnKeyListener true
                }
            }
            return@setOnKeyListener false
        }
    }


    var dpadListener: ((Int) -> Unit)? = null
    fun setUpListener(e: (Int) -> Unit) {
        this.dpadListener = e
    }

}